package com.shopping.mall.core.module.company.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.company.entity.Company;
import com.shopping.mall.core.module.company.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "Company")
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @ApiOperation("返回最新插入的")
    @GetMapping("/last")
    @UnLogin
    public Result<Object> last(){
        return companyService.last();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Company>> getCompanyPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Company company) {
        IPage<Company> companyPage=companyService.getCompanyPage(current,size,company);
        return Result.OK(companyPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<Company> update(@Validated(Company.update.class) @RequestBody Company company) {
        companyService.updateById(company);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    @PostMapping
    public Result<Object> add(@Validated(Company.insert.class) @RequestBody Company company) {
        companyService.save(company);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Company> getById(@RequestParam String id){
        Company company=companyService.getById(id);
        return Result.OK(company);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Company>> getAll(Company company){
        List<Company> companyList=companyService.getAll(company);
        return Result.OK(companyList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return companyService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return companyService.batchDelete(ids);
    }
}
